package com.moxiao.yfjh.module.activity.service.packageInfo;

import com.moxiao.yfjh.module.activity.dal.dataobject.productInfo.ProductInfoDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.activity.controller.admin.packageInfo.vo.*;
import com.moxiao.yfjh.module.activity.dal.dataobject.packageInfo.PackageInfoDO;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.activity.convert.packageInfo.PackageInfoConvert;
import com.moxiao.yfjh.module.activity.dal.mysql.packageInfo.PackageInfoMapper;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.activity.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲套餐信息 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class PackageInfoServiceImpl implements PackageInfoService {

    @Resource
    private PackageInfoMapper packageInfoMapper;

    @Override
    public Long createPackageInfo(PackageInfoCreateReqVO createReqVO) {
        // 插入
        PackageInfoDO packageInfo = PackageInfoConvert.INSTANCE.convert(createReqVO);
        packageInfoMapper.insert(packageInfo);
        // 返回
        return packageInfo.getId();
    }

    @Override
    public void updatePackageInfo(PackageInfoUpdateReqVO updateReqVO) {
        // 校验存在
        validatePackageInfoExists(updateReqVO.getId());
        // 更新
        PackageInfoDO updateObj = PackageInfoConvert.INSTANCE.convert(updateReqVO);
        packageInfoMapper.updateById(updateObj);
    }

    @Override
    public void deletePackageInfo(Long id) {
        // 校验存在
        validatePackageInfoExists(id);
        // 删除
        packageInfoMapper.deleteById(id);
    }

    private void validatePackageInfoExists(Long id) {
        if (packageInfoMapper.selectById(id) == null) {
            throw exception(PACKAGE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public PackageInfoDO getPackageInfo(Long id) {
        return packageInfoMapper.selectById(id);
    }

    @Override
    public List<PackageInfoDO> getPackageInfoList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return packageInfoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PackageInfoDO> getPackageInfoPage(PackageInfoPageReqVO pageReqVO) {
        return packageInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PackageInfoDO> getPackageInfoList(PackageInfoExportReqVO exportReqVO) {
        return packageInfoMapper.selectList(exportReqVO);
    }

    @Override
    public PackageInfoDO getPackageInfoTodayLast(Date todayStart, Date todayEnd) {
        LambdaQueryWrapper<PackageInfoDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(PackageInfoDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.orderByDesc(PackageInfoDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        return packageInfoMapper.selectOne(lqw);
    }

    @Override
    public Integer getPackageInfoNo(String activeNo) {
        return packageInfoMapper.getPackageInfoNo(activeNo);
    }

}
